package com.neuedu.controller;


import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.User;
import com.neuedu.service.UserService;
import com.neuedu.service.impl.UserServiceImpl;
import com.neuedu.utils.R;

import java.io.*;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class UserController {

    public static final String FILE_PATH="C:\\wuyetupian";

    private UserService userService=new UserServiceImpl();

    public R uploadIcon(JSONObject jsonObject){
        //需要接收两个参数
        //上传一个图片，服务器保存，然后返回一个文件名而已。
        String base64=jsonObject.getString("base64");

        //1、base64还原为图片，保存至服务器的硬盘
        byte[] bytes = Base64.getDecoder().decode(base64);
        //写文件，文件名应该叫啥？
        String fileName= UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
        fileName=fileName+".png";
        FileOutputStream fos=null;
        try {
            fos =new FileOutputStream(new File(FILE_PATH+File.separator+fileName));
            fos.write(bytes);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return R.ok().setData(fileName);
    }

    public R editSelfInfo(JSONObject jsonObject){
        Integer id=jsonObject.getInteger("userId");
        String username=jsonObject.getString("username");
        String password=jsonObject.getString("password");
        String telephone=jsonObject.getString("telephone");
        String newPassword=jsonObject.getString("newPassword");
        String icon=jsonObject.getString("icon");

        //1、password不是“”
        User user=userService.findUserById(id);
        if(password!=null&&!"".equals(password)){
            if(password.equals(user.getPassword())){
                user.setPassword(newPassword);
            }else{
                return R.error("旧密码输入错误");
            }
        }

        user.setUsername(username);
        user.setTelephone(telephone);
        if(icon!=null&&!"".equals(icon)){
            user.setIcon(icon);
        }
        //保存数据库
        userService.updateUserById(user);

        return R.ok("更新成功");
    }


    public R signIn(JSONObject jsonObject){
        String username=jsonObject.getString("username");
        String password=jsonObject.getString("password");
        User user = userService.findUserBySignInfo(username, password);
        if(user==null){
            return R.error("用户名/密码错误");
        }else{
            return R.ok("登录成功").setData(user);
        }
    }

    public R showImg(Map<String,String[]> map){
        String fileName = map.get("fileName")[0];
        File file=new File(FILE_PATH+ File.separator + fileName);
        FileInputStream fis=null;
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        try {
            fis=new FileInputStream(file);
            byte [] bytes=new byte [1024*8];
            int len=0;
            while ((len=fis.read(bytes))!=-1){
                baos.write(bytes,0,len);
            }
            byte[] imageByte = baos.toByteArray();
            String base64 = Base64.getEncoder().encodeToString(imageByte);
            return R.ok().setData(base64);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("图片展示失败");
    }

    public R selectUserAll(Map<String,String[]> map){
        List<User> users = userService.selectUserAll();
        return R.ok().setData(users);
    }
}
